{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RFdiffusion All-Atom (RFdiffusionAA)本地部署\n",
    "\n",
    "## 相关链接\n",
    "\n",
    "- [Rohith Krishna et al. ,Generalized biomolecular modeling and design with RoseTTAFold All-Atom.Science384,eadl2528(2024).DOI:10.1126/science.adl2528](https://www.science.org/doi/10.1126/science.adl2528)\n",
    "- [github官方仓库](https://github.com/baker-laboratory/rf_diffusion_all_atom)\n",
    "\n",
    "## 开源协议:BSD\n",
    "\n",
    "## 介绍\n",
    "\n",
    "RFdiffusion All-Atom (RFdiffusionAA)是RFAA对蛋白-小分子体系的微调模型,旨在提高蛋白-小分子体系的预测精度.\n",
    "\n",
    "## RFDIAA本地部署\n",
    "\n",
    "用系统包管理器或从源代码[安装apptainer](https://apptainer.org/docs/admin/main/installation.html).\n",
    "\n",
    "然后在终端执行:\n",
    "\n",
    "```bash\n",
    "git clone https://github.com/baker-laboratory/rf_diffusion_all_atom.git ~/git_develop/RFDIAA # 克隆路径自行修改\n",
    "cd ~/git_develop/RFDIAA\n",
    "aria2c -x 16 -s 16 http://files.ipd.uw.edu/pub/RF-All-Atom/containers/rf_se3_diffusion.sif # 下载容器,11G\n",
    "aria2c -x 16 -s 16 http://files.ipd.uw.edu/pub/RF-All-Atom/weights/RFDiffusionAA_paper_weights.pt # 下载模型权重,1.2G\n",
    "git submodule init\n",
    "git submodule update\n",
    "```\n",
    "\n",
    "### 小分子结合蛋白预测\n",
    "\n",
    "要从7v11生成配体OQO的binder:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from note_utils.path import expand_path,chdir\n",
    "\n",
    "RFAA_REPO = expand_path('~/git_develop/RFDIAA') # RFDIAA仓库路径自行修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with chdir(RFAA_REPO):\n",
    "    !apptainer run --nv rf_se3_diffusion.sif -u run_inference.py inference.deterministic=True diffuser.T=100 inference.output_prefix=output/ligand_only/sample inference.input_pdb=input/7v11.pdb contigmap.contigs=[\\'150-150\\'] inference.ligand=OQO inference.num_designs=1 inference.design_startnum=0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参数:\n",
    "\n",
    "- 不使用GPU的情况下,必须移除--nv选项\n",
    "- inference.deterministic=True 固定随机数种子,与inference.design_startnum=X一起生效,确保可重复性.pytorch无法保证CPU/GPU混合架构的可重复性\n",
    "- inference.num_designs=1 生成一个设计(预测)\n",
    "- contigmap.contigs=[\\'150-150\\'] 指定生成蛋白的长度\n",
    "- diffuser.T=100 指定降噪步骤\n",
    "\n",
    "输出:\n",
    "\n",
    "- output/ligand_only/sample_0.pdb 设计的蛋白\n",
    "- output/ligand_only/sample_0_Xt-1_traj.pdb 部分降噪的中间结构\n",
    "- output/ligand_only/sample_0_X0-1_traj.pdb 每一步网络生成的真实性预测\n",
    "\n",
    "要注意这些结构相关的序列没有意义,除非是给定的一些模体.必须要进行结构预测或者表达,必须使用LigandMPNN或者相似的工具来生成骨架序列.\n",
    "\n",
    "要在模体中包含蛋白残基A430-435,使用参数`contigmap.contigs`.例如`contigmap.contigs=[\\'10-120,A84-87,10-120\\']`告诉模型,设计包含两侧各有10-120个残基的4残基模体A84-87.\n",
    "\n",
    "### 考虑蛋白模体的小分子binder设计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with chdir(RFAA_REPO):\n",
    "    !apptainer run --nv rf_se3_diffusion.sif -u run_inference.py inference.deterministic=True diffuser.T=200 inference.output_prefix=output/ligand_protein_motif/sample inference.input_pdb=input/1haz.pdb contigmap.contigs=[\\'10-120,A84-87,10-120\\'] contigmap.length=\"150-150\" inference.ligand=CYC inference.num_designs=1 inference.design_startnum=0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "端到端的设计流程,可以参考<https://github.com/ikalvet/heme_binder_diffusion>."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "RFAA",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
